﻿Public Class ImmoveableForm
    Inherits System.Windows.Forms.Form

    Private Declare Function EnableMenuItem Lib "user32.dll" _
    Alias "EnableMenuItem" (ByVal hMenu As IntPtr, _
    ByVal uIDEnableItem As Int32, ByVal uEnable As Int32) As Int32

    Private Const HTCAPTION As Int32 = &H2

    Private Const MF_BYCOMMAND As Int32 = &H0&
    Private Const MF_ENABLED As Int32 = &H0&
    Private Const MF_GRAYED As Int32 = &H1&
    Private Const MF_DISABLED As Int32 = &H2&

    Private Const SC_MOVE As Int32 = &HF010&

    Private Const WM_NCLBUTTONDOWN As Int32 = &HA1
    Private Const WM_SYSCOMMAND As Int32 = &H112
    Private Const WM_INITMENUPOPUP As Int32 = &H117&

    Private bMoveable As Boolean = True

    Public Sub New()
        MyBase.New()
    End Sub

    <System.ComponentModel.Category("Behavior"), _
    System.ComponentModel.Description("Allows the form to be moved")> _
    Public Overridable Property Moveable() As Boolean
        Get
            Return bMoveable
        End Get
        Set(ByVal Value As Boolean)
            If bMoveable <> Value Then
                bMoveable = Value
            End If
        End Set
    End Property

    Protected Overrides Sub WndProc( _
    ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_INITMENUPOPUP Then
            ' Thụ lý việc hiển thị menu hệ thống.
            If m.LParam.ToInt32 \ 65536 <> 0 Then
                Dim AbleFlags As Int32 = MF_ENABLED
                If Not Moveable Then AbleFlags = MF_DISABLED Or MF_GRAYED
                EnableMenuItem(m.WParam, SC_MOVE, _
                MF_BYCOMMAND Or AbleFlags)
            End If
        End If

        If Not Moveable Then
            If m.Msg = WM_NCLBUTTONDOWN Then
                ' Không cho phép kéo rê cửa sổ bằng thanh tiêu đề.
                If m.WParam.ToInt32 = HTCAPTION Then
                    Return
                End If
            End If
            If m.Msg = WM_SYSCOMMAND Then
                ' Vô hiệu chức năng Move trên menu hệ thống.
                If (m.WParam.ToInt32 And &HFFF0) = SC_MOVE Then
                    Return
                End If
            End If
        End If
        MyBase.WndProc(m)
    End Sub

End Class